
package com.crowdfunding.circle.data.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSONObject;
import com.crowdfunding.circle.common.controller.BaseController;
import com.crowdfunding.circle.common.param.SessionUserInfo;
import com.crowdfunding.circle.data.entity.ProjectDataVO;
import com.crowdfunding.circle.data.param.ProjectDataParam;
import com.crowdfunding.circle.data.service.ProjectDataService;

/**
 * 平台数据Controller
 *
 * @author 朱国军
 * @since 1.0
 * @version 2015年12月27日 朱国军
 */
@Controller
public class ProjectDataController extends BaseController {
    
    private Logger logger = LoggerFactory.getLogger(ProjectDataController.class);
    
    @Autowired
    private ProjectDataService projectDataService;
    
    private final static String LIST_PAGE = "/data/data_list_ajax";
    
    private final static String VIEW_PAGE = "/data/data_view";
    
    private final static String UPDATE_PAGE = "/data/data_edit";

    private final static String ADMIN_LIST_PAGE = "/admin/data_manage";
    
    @RequestMapping(value = "/admin/data/toInsert")
    public ModelAndView intoInsert() {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        ProjectDataVO projectDataVO = new ProjectDataVO();
        resultMap.put("projectDataVO", projectDataVO);
        return new ModelAndView(UPDATE_PAGE, resultMap);
    }

    @RequestMapping(value = "/admin/data/toUpdate/{id}")
    public ModelAndView intoUpdate(@PathVariable String id) {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        ProjectDataVO projectDataVO = projectDataService.findById(id);
        resultMap.put("projectDataVO", projectDataVO);
        return new ModelAndView(UPDATE_PAGE, resultMap);
    }

    @RequestMapping(value = "/admin/data/update", produces = "application/json;charset=utf-8;")
    @ResponseBody
    public String update(ProjectDataVO projectDataVO) {
        
        SessionUserInfo seUser = (SessionUserInfo) session.getAttribute("sessionUserInfo");
        
        Map<String, Object> resultMap = new HashMap<String, Object>();
        try {

            Integer affectCount = 0;
            if (projectDataVO != null && StringUtils.isNotBlank(projectDataVO.getProjectDataId())) {
                affectCount = projectDataService.update(projectDataVO);
            } else if (projectDataVO != null) {
                projectDataVO.setProjectDataId(UUID.randomUUID().toString());
                projectDataVO.setDataStatus(0);
                projectDataVO.setPublishUser(seUser.getUserId());
                affectCount = projectDataService.insert(projectDataVO);
            }
            
            if (affectCount != null && affectCount == 1) {
                resultMap.put("result", true);
                resultMap.put("message", "新增或修改平台数据成功");
            } else {
                resultMap.put("result", false);
                resultMap.put("message", "新增或修改平台数据失败");
            }
        } catch (Exception e) {
            logger.error("新增或修改平台数据出错", e);
            resultMap.put("result", false);
            resultMap.put("message", "新增或修改平台数据出错：" + e.getLocalizedMessage());
        }
        return JSONObject.toJSONString(resultMap);
    }

    @RequestMapping(value = "/check/data/delete/{id}", produces = "application/json;charset=utf-8;")
    @ResponseBody
    public String delete(@PathVariable String id) {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        try {
            Integer deleteCount = projectDataService.delete(id);
            if (deleteCount != null && deleteCount == 1) {
                resultMap.put("result", true);
                resultMap.put("message", "删除平台数据成功");
            } else {
                resultMap.put("result", false);
                resultMap.put("message", "删除平台数据失败");
            }
        } catch (Exception e) {
            logger.error("删除平台数据出错", e);
            resultMap.put("result", false);
            resultMap.put("message", "删除平台数据出错：" + e.getLocalizedMessage());
        }
        return JSONObject.toJSONString(resultMap);
    }
    
    @RequestMapping(value = "/uncheck/data/list")
    public ModelAndView queryList(ProjectDataParam projectDataParam) {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        List<ProjectDataVO> dataList = projectDataService.queryByPage(projectDataParam);
        resultMap.put("dataList", dataList);
        resultMap.put("paramMap", projectDataParam);
        return new ModelAndView(LIST_PAGE, resultMap);
    }
    
    @RequestMapping(value = "/admin/data/list")
    public ModelAndView queryAdminList(ProjectDataParam projectDataParam) {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        List<ProjectDataVO> dataList = projectDataService.queryByPage(projectDataParam);
        resultMap.put("dataList", dataList);
        resultMap.put("paramMap", projectDataParam);
        return new ModelAndView(ADMIN_LIST_PAGE, resultMap);
    }

    @RequestMapping(value = "/uncheck/data/view/{id}")
    public ModelAndView viewConfigInfo(@PathVariable String id) {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        ProjectDataVO projectDataVO = projectDataService.findById(id);
        resultMap.put("projectDataVO", projectDataVO);
        return new ModelAndView(VIEW_PAGE, resultMap);
    }
}
